package com.kevin.concurrent;

/**
 * 测试Thread.yield()方法
 * @author Kevin Liu
 * @date 2020/10/1 9:05 下午
 */
public class Third extends Thread{

    public Third(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() +" START");
        for (int i = 0; i < 5; i++) {
            System.out.println("" + this.getName() + "-----" + i);
            // 当i为30时，该线程就会把CPU时间让掉，让其他或者自己的线程执行（也就是谁先抢到谁执行）
            if (i % 5 == 0) {
                Thread.yield();
            }
        }
        System.out.println(Thread.currentThread().getName()+"END");
    }

    public static void main(String[] args) {
        new Third("FIRST").start();
        new Third("SECOND").start();
        new Third("THIRD").start();
    }
}
